import { STANDARD_CYCLE_TIME } from "../../../constant";
import { AbstractPlantStruct } from "./AbstractPlantStruct";
import { PlanetProduct } from "./PlanetProduct";
import type { PlanetPin, PlanetRoute } from "@scvzerng/esi-163/dist/types/esi/plants/model/PlanetLayout";
import type { UniverseType } from "../../../sde/entity";

/**
 * 加工厂
 */
export class PlanetFactory extends AbstractPlantStruct {
  contents: PlanetProduct[];
  /**
   * 输入
   */
  inputs: PlanetProduct[];
  /**
   * 输出
   */
  output?: PlanetProduct;
  /**
   * 最后一次循环时间
   */
  lastCycleStart?: string;
  /**
   * 生产周期
   */
  cycleTime: number;
  constructor(pin: PlanetPin, type: UniverseType, routes: (PlanetRoute & { type: UniverseType })[]) {
    super(pin, type);
    this.lastCycleStart = pin.last_cycle_start;
    this.cycleTime = STANDARD_CYCLE_TIME;
    //高级工厂和高科技工厂的周期是1小时
    if (type.name.includes("高")) {
      this.cycleTime = STANDARD_CYCLE_TIME * 2;
    }
    const factor = STANDARD_CYCLE_TIME / this.cycleTime;
    const output = routes
      .filter((route) => route.source_pin_id === pin.pin_id)
      .map((route) => new PlanetProduct(route.quantity, route.type, Math.ceil(route.quantity * factor)))[0];
    this.inputs = routes
      .filter((route) => route.destination_pin_id === pin.pin_id)
      .map((route) => new PlanetProduct(route.quantity, route.type, Math.ceil(route.quantity * factor)));
    this.output = output;
    this.contents = pin.contents?.map((e: any) => new PlanetProduct(e.amount, e.type)) ?? [];
  }
}
